home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_187 / mackie / mackie.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  42KB  |  1,563 lines

  1. /*
  2.     Modified to work with Manx 3.6a; probably won't work with Lattice.
  3.                                 Based on:
  4.                                                                              */
  5. /*  Compile with -DSPLINES for splines instead of lines                      */
  6. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  7. /* |_o_o|\\ Copyright (c) 1986 The Software Distillery.  All Rights Reserved */
  8. /* |. o.| || This program may not be distributed without the permission of   */
  9. /* | .  | || the authors.                                                    */
  10. /* | o  | ||    Dave Baker     Ed Burnette  Stan Chow    Jay Denebeim        */
  11. /* |  . |//     Gordon Keener  Jack Rouse   John Toebes  Doug Walker         */
  12. /* ======          BBS:(919)-471-6436      VOICE:(919)-469-4210              */ 
  13. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  14. /*
  15.  * VERY loosely based on the input.device example by Rob Peck, 12/1/85
  16.  */
  17. /* * * * * * * * * INCLUDE FILES * * * * * * * * * * * */
  18. #include <exec/types.h>
  19. #include <exec/nodes.h>
  20. #include <exec/lists.h>
  21. #include <exec/memory.h>
  22. #include <exec/interrupts.h>
  23. #include <exec/ports.h>
  24. #include <exec/libraries.h>
  25. #include <exec/io.h>
  26. #include <exec/tasks.h>
  27. #include <exec/execbase.h>
  28. #include <exec/devices.h>
  29. #include <devices/timer.h>
  30. #include <devices/input.h>
  31. #include <devices/inputevent.h>
  32. #include <devices/console.h>
  33. #include <intuition/intuition.h>
  34. #include <intuition/intuitionbase.h>
  35. #include <libraries/dos.h>
  36. #include <libraries/dosextens.h>
  37. #include <graphics/gfxmacros.h>
  38. #include <hardware/custom.h>
  39. #include <hardware/dmabits.h>
  40. /*
  41.  *   I need this kludge because of a bug in functions.h.
  42.  */
  43. #define ConsoleDevice IDontReallyExist
  44. #include <functions.h>
  45. #undef ConsoleDevice
  46. #include <stdio.h>
  47.  
  48. /* * * * * * * * * * * CONSTANTS * * * * * * * * * * * * */
  49. #define PORTNAME     "Mackie.port"
  50. #define TIMEINTERVAL 1L      /* in seconds */
  51. #define DEFTIME      300     /* two minute timeout */
  52. #define MAXCMD       200
  53. #define MAXPENDINGSYSTEM 20
  54. #define DEFKEY    0x45
  55. #define DEFCMD    "NEWCLI >NIL: <NIL:"
  56. #define STARTUPFILE "s:.mackierc"
  57. /*
  58.  *   Macro to make BPTR things easier to work with.
  59.  */
  60. #define BSTRtoS(a) ((char *)(((long)(a))<<2))
  61. /* * * * * * * * * * * GLOBAL VARIABLES * * * * * * * * * */
  62. /*
  63.  *   Detach stuff (doesn't work, somehow!)
  64.  */
  65. struct Task          *globalbuddy ;
  66. ULONG                 globalcreatclisig ;
  67. ULONG                 globalunblanksig ;
  68. ULONG                 globalfrontsig ;
  69. ULONG                 globalnoevents ;
  70. short                 globalcreatsignum ;
  71. short                 globalblanksignum ;
  72. short                 globalreplysignum ;
  73. short                 globalfrontsignum ;
  74. short                 globalkey ;
  75. char                  globalfrontkey[MAXPENDINGSYSTEM] ;
  76. char                  globalfrontqual[MAXPENDINGSYSTEM] ;
  77. short                 globalfrontptr ;
  78. short                 globaldraw ;
  79. short                 globalhelppressed ;
  80. struct Screen        *globalblankscreen ;
  81. #define SHIFT 1
  82. #define NOTCLI 2
  83. #define NOTINTUITION 4
  84. struct hotkey {
  85.    struct hotkey*next ;
  86.    char key, flags ;
  87.    int structlen ;
  88.    char *matchstring, *startstring ;
  89.    char strings[2] ;
  90. } *hotkeys ;
  91. struct Window *lastwindows[200] ;
  92. struct Task *task ;
  93. long taskreply ; /* the signal the line drawing task will return */
  94. #define STACKSIZE (1000)
  95. long stackmem[STACKSIZE/4] ;
  96. struct InfoData *infoptr ;
  97. struct MsgPort *FindPort(), *CreatePort();
  98. void DeletePort();
  99. char *startupfile = STARTUPFILE ;
  100. struct OURMSG {
  101.  struct Message msgpart;
  102.  short key;
  103.  short interval;
  104.  short draw ;
  105.  char cmd[MAXCMD];
  106.  };
  107.  
  108. extern char *strcpy() ;
  109.  
  110. /************************************************************************/
  111. /* the handler subroutine - called through the handler stub             */
  112. /************************************************************************/
  113. extern void HandlerInterface();
  114. void foobar() {
  115. #asm
  116. _HandlerInterface:
  117.     movem.l a4,-(a7)
  118.     movem.L    A0/A1,-(A7)
  119.     jsr    _geta4#
  120.     jsr    _myhandler
  121.     addq.L    #8,A7
  122.     movem.L    (a7)+,a4
  123.     rts
  124. #endasm
  125. }
  126. char keytoasc[128] ;
  127. struct InputEvent *myhandler(ev, gptr)
  128. struct InputEvent *ev;      /* and a pointer to a list of events */
  129. long gptr ; /* ignore */
  130. {
  131.    register struct InputEvent *ep, *laste;
  132.    int key ;
  133.    /*
  134.     * run down the list of events
  135.     * to see if they pressed
  136.     * one of the magic buttons
  137.     */
  138.    for (ep = ev, laste = NULL; ep != NULL; ep = ep->ie_NextEvent) {
  139.       if ((ep->ie_Class == IECLASS_RAWKEY)    &&
  140.              (((ep->ie_Qualifier & IEQUALIFIER_LCOMMAND) &&
  141.                (ep->ie_Code == globalkey)) ||
  142.               (!globalhelppressed && ep->ie_Code == 0x5f) ||
  143.               (globalhelppressed &&
  144.                (ep->ie_Code & 0x80) == 0 &&
  145. /*
  146.  *   All the qualifiers have code = 0x6?; dangerous to take advantage
  147.  *   of?
  148.  */
  149.                (ep->ie_Code & 0xf0) != 0x60 &&
  150.                globalfrontkey[globalfrontptr] == 0 &&
  151.                (globalhelppressed = (keytoasc[ep->ie_Code] != '.'))))) {
  152.          if ((ep->ie_Qualifier & IEQUALIFIER_LCOMMAND) &&
  153.               (ep->ie_Code == globalkey))
  154.             key = -1 ;
  155.          else if (globalhelppressed) {
  156.             globalfrontkey[globalfrontptr] = key = ep->ie_Code ;
  157.             globalfrontqual[globalfrontptr] = ep->ie_Qualifier ;
  158.             globalfrontptr++ ;
  159.             if (globalfrontptr >= MAXPENDINGSYSTEM)
  160.                globalfrontptr = 0 ;
  161.             globalhelppressed = 0 ;
  162.          } else {
  163.             globalhelppressed = 1 ;
  164.             key = 0 ;
  165.          }
  166.          /* we can handle this event so take it off the chain */
  167.          if (laste == NULL)
  168.             ev = ep->ie_NextEvent;
  169.          else
  170.             laste->ie_NextEvent = ep->ie_NextEvent;
  171.          /* now tell him to create the new cli */
  172.          if (key == -1)
  173.             Signal(globalbuddy, globalcreatclisig);
  174.          else if (key > 0)
  175.             Signal(globalbuddy, globalfrontsig);
  176.       } else
  177.          laste = ep;
  178.       if (ep->ie_Class != IECLASS_TIMER) {
  179.          globalnoevents = 0;
  180.          if (globalblankscreen != NULL)
  181.             Signal(globalbuddy, globalunblanksig);
  182.       }
  183.    }
  184.    /* pass on the pointer to the event */
  185.    return(ev);
  186. }
  187.  
  188. /* * * * * * * * * * * EXTERNAL ROUTINES * * * * * * * * * */
  189. struct IntuitionBase *IntuitionBase;
  190. struct GfxBase       *GfxBase;
  191. struct DosLibrary    *DosBase;
  192. struct NewScreen      NewScreen = 
  193.    { 0, 0, 320, 30, 1, 0, 1, NULL, CUSTOMSCREEN, NULL, NULL, NULL, NULL };
  194.  
  195. extern struct MsgPort  *CreatePort();
  196. struct IOStdReq *CreateIOReq();
  197. void DeleteIOReq();
  198.  
  199. /************************************************************************/
  200. /* Queue a timer to go off in a given number of seconds                 */
  201. /************************************************************************/
  202. void QueueTimer(tr,seconds)
  203. register struct timerequest *tr;
  204. ULONG seconds;
  205. {
  206.    tr->tr_node.io_Command = TR_ADDREQUEST;   /* add a new timer request */
  207.    tr->tr_time.tv_secs =  seconds;            /* seconds */
  208.    tr->tr_time.tv_micro = 0;
  209.    SendIO( (struct IORequest *)tr );
  210. }
  211. /*
  212.  *   A utility to change our priority.
  213.  */
  214. prito(n)
  215. int n ;
  216. {
  217.    SetTaskPri(FindTask(0L), (long)n) ;
  218. }
  219. /************************************************************************/
  220. /* the main program to do the popcli stuff                              */
  221. /************************************************************************/
  222. main(argc, argv)
  223. int argc ;
  224. char *argv[] ;
  225. {
  226.    struct MsgPort *port;
  227.    int stay = 0;
  228.    register struct OURMSG *msg;
  229.    int i ;
  230.    char cmdstr[MAXCMD];
  231.    short key, timeout ;
  232.    struct FileHandle *nullfh = NULL ;
  233.    ULONG sig, timersig;
  234.    struct timerequest *timerreq;
  235.    struct MsgPort     *timerport;
  236.    struct MsgPort     *inputDevPort;
  237.    struct IOStdReq    *inputRequestBlock;
  238.    register struct Interrupt handlerStuff;
  239.    char *cmd ;
  240.    int draw = 0 ;
  241.    int nextfront = 0 ;
  242.  
  243.    prito(20) ;
  244.    globalcreatsignum  = -1;
  245.    globalblanksignum  = -1;
  246.    globalreplysignum  = -1;
  247.    globalfrontsignum  = -1;
  248.    globalblankscreen  = NULL;
  249.    globalkey          = DEFKEY ;
  250.    globaldraw         = 1 ;
  251.    timerreq            = NULL;
  252.    timerport           = NULL;
  253.    inputDevPort        = NULL;
  254.    inputRequestBlock   = NULL;
  255.    /* now see if we are already installed */
  256.    if ((port = FindPort(PORTNAME)) == NULL) {
  257.       stay = 1; /* remember to hang around when we are done */
  258.       /* not installed, we need to install our own port */
  259.       if ((port = CreatePort(PORTNAME,0L)) == NULL)
  260.          goto abort;
  261.    }
  262. /*
  263.  *   If we are hanging around, initialize our keyboard translation table.
  264.  *   If we have difficulty, exit angry.
  265.  */
  266.    if (stay)
  267.       if (initkeytoasc())
  268.          goto abort ;
  269.    /* now send the parameter to the waiting program */
  270.    if ((msg = (struct OURMSG *)
  271.        AllocMem((long)sizeof(struct OURMSG), MEMF_CLEAR|MEMF_PUBLIC)) == NULL)
  272.       goto abort;
  273.    if ((infoptr = (struct InfoData *)
  274.       AllocMem((long)sizeof(struct InfoData), MEMF_CLEAR)) == NULL)
  275.       goto abort ;
  276.    /* fill in the message information */
  277.    msg->msgpart.mn_Length = sizeof(struct OURMSG);
  278.    strcpy(cmdstr, DEFCMD);
  279.    timeout = -1 ;
  280.    key = 0 ;
  281.    msg->cmd[0] = 0;
  282.    /* if we were run from CLI then output our banner and process parameters */
  283.    if (argc > 0) {
  284.       /* display our copyright */
  285.       if (stay)
  286.          puts("Mackie 1.13 by Tomas Rokicki - Copyright \xa9 1987, 1988 Radical Eye Software") ;
  287.       if (argc == 1)
  288.          puts("Usage: Mackie [-q] [-l] [-b] [-f startup] [time] [\"command\"]") ;
  289.       argc-- ;
  290.       argv++ ;
  291.       while (argc > 0) {
  292.          cmd = argv[0] ;
  293.          if (*cmd == '-') {
  294.             cmd++ ;
  295.             switch (*cmd) {
  296. case 'q' : case 'Q' :
  297.                key = -1 ;
  298.                puts("\x9B1mMackie\x9B0m Terminating") ;
  299.                break ;
  300. case 'l' : case 'L' :
  301.                draw = 1 ;
  302.                break ;
  303. case 'b' : case 'B' :
  304.                draw = -1 ;
  305.                break ;
  306. case 'f' : case 'F' :
  307.                if (cmd[1])
  308.                   startupfile = cmd + 1 ;
  309.                else {
  310.                   argv++ ;
  311.                   argc-- ;
  312.                   startupfile = argv[0] ;
  313.                }
  314.                break ;
  315. default :
  316.                puts("Error in parameter!") ;
  317.             }
  318.          } else if ('0' <= *cmd && *cmd <= '9') {
  319.             timeout = 0;
  320.             while ((*cmd >= '0') && (*cmd <= '9'))
  321.                timeout = (timeout*10) + *cmd++ - '0';
  322.             if (timeout < 0)
  323.                timeout = DEFTIME;
  324.          } else {
  325.             strcpy(msg->cmd, cmd) ;
  326.          }
  327.          argc-- ;
  328.          argv++ ;
  329.       }
  330.    }
  331.    msg->interval = timeout;
  332.    msg->key = key;
  333.    msg->draw = draw ;
  334.    if (stay)
  335.       processstartup(startupfile, msg) ;
  336.    if (draw)
  337.       globaldraw = draw ;
  338.    PutMsg(port,(struct Message *)msg);
  339.    if (!stay) goto abort;
  340.    if (timeout == -1)
  341.       timeout = DEFTIME ;
  342.    globalblankscreen = NULL;
  343.    globalbuddy = FindTask(0L);
  344.    globalnoevents = 0;
  345.    nullfh = Open("NIL:", MODE_NEWFILE);
  346.    if (((inputDevPort = CreatePort(0L,0L)) == NULL) ||
  347.       ((inputRequestBlock =
  348.           CreateIOReq(inputDevPort, (long)sizeof(struct IOStdReq))) == NULL) ||
  349.       ((timerport = CreatePort(0L,0L)) == NULL) ||
  350.       ((timerreq  = (struct timerequest *)
  351.           CreateIOReq(timerport, (long)sizeof(struct timerequest))) == NULL))
  352.          goto abort ;
  353.    if (((globalcreatsignum = AllocSignal(-1L)) == -1) ||
  354.       ((globalblanksignum = AllocSignal(-1L)) == -1) ||
  355.       ((globalreplysignum = AllocSignal(-1L)) == -1) ||
  356.       ((globalfrontsignum = AllocSignal(-1L)) == -1) ||
  357.       ((GfxBase = (struct GfxBase *)
  358.                   OpenLibrary("graphics.library", 0L)) == NULL) ||
  359.       ((IntuitionBase = (struct IntuitionBase *)
  360.                         OpenLibrary("intuition.library", 0L)) == NULL) ||
  361.       OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)timerreq, 0L) ||
  362.       OpenDevice("input.device",0L,(struct IORequest *)inputRequestBlock,0L))
  363.          goto abort;
  364.    handlerStuff.is_Data = (APTR)0L ;
  365.    handlerStuff.is_Code = HandlerInterface;
  366.    handlerStuff.is_Node.ln_Pri = 51;
  367.    timersig            = (1L << timerport->mp_SigBit);
  368.    globalcreatclisig  = 1L << globalcreatsignum;
  369.    globalunblanksig   = 1L << globalblanksignum;
  370.    globalfrontsig     = 1L << globalfrontsignum;
  371.    inputRequestBlock->io_Command = IND_ADDHANDLER;
  372.    inputRequestBlock->io_Data    = (APTR)&handlerStuff;
  373.    DoIO((struct IORequest *)inputRequestBlock);
  374.    QueueTimer(timerreq, TIMEINTERVAL);
  375.    for(;;) {         /* FOREVER */
  376.       sig = Wait( globalcreatclisig | globalunblanksig | timersig |
  377.                   globalfrontsig);
  378.       /* see if they asked us to change the interval */
  379.       if ((msg = (struct OURMSG *)GetMsg(port)) != NULL) {
  380.          if (msg->cmd[0]) strcpy(cmdstr, msg->cmd);
  381.          if (msg->key)
  382.             globalkey = msg->key;
  383.          if (msg->interval != -1)
  384.             timeout    = msg->interval;
  385.          if (msg->draw)
  386.             globaldraw = msg->draw ;
  387.          FreeMem((char *)msg, (long)msg->msgpart.mn_Length);
  388.          if (msg->key == -1) goto abort;
  389.       }
  390.       if ((sig & globalunblanksig) && globalblankscreen)
  391.          screenunblank() ;
  392.       if (sig & globalcreatclisig) {
  393.          WBenchToFront();
  394.          prito(0) ;
  395.          (void)Execute(cmdstr,nullfh,nullfh);
  396.          prito(20) ;
  397.       }
  398.       if (sig & globalfrontsig) {
  399.          while (i=globalfrontkey[nextfront]) {
  400.             windowtofront(keytoasc[i], globalfrontqual[nextfront]) ;
  401.             globalfrontkey[nextfront] = 0 ;
  402.             nextfront++ ;
  403.             if (nextfront >= MAXPENDINGSYSTEM)
  404.                nextfront = 0 ;
  405.          }
  406.       }
  407.       if (sig & timersig) {
  408.          /* get rid of the message */
  409.          (void)GetMsg(timerport);
  410.          QueueTimer(timerreq, TIMEINTERVAL);
  411.          if (task)
  412.             SetTaskPri(task, 10L) ;
  413.          if (timeout != 0 && (globalnoevents++ >= timeout) && (globalblankscreen == NULL))
  414.             blankscreen() ;
  415.       }
  416.    }
  417. abort:
  418.    if (infoptr) {
  419.       FreeMem(infoptr, (long)sizeof(struct InfoData)) ;
  420.       infoptr = NULL ;
  421.    }
  422.    if (timerreq != NULL) {
  423.       if (timerreq->tr_node.io_Device != NULL)
  424.          CloseDevice((struct IORequest *)timerreq);
  425.       DeleteIOReq((struct IOStdReq *)timerreq);
  426.    }
  427.    if (inputRequestBlock != NULL) {
  428.       if (inputRequestBlock->io_Device != NULL) {
  429.          inputRequestBlock->io_Command = IND_REMHANDLER;
  430.          inputRequestBlock->io_Data = (APTR)&handlerStuff;
  431.          DoIO((struct IORequest *)inputRequestBlock);
  432.          CloseDevice((struct IORequest *)inputRequestBlock);
  433.       }
  434.       DeleteIOReq(inputRequestBlock);
  435.    }
  436.    screenunblank() ;
  437.    if (timerport != NULL)          DeletePort(timerport);
  438.    if (globalcreatsignum != -1)   FreeSignal(globalcreatsignum);
  439.    if (globalblanksignum != -1)   FreeSignal(globalblanksignum);
  440.    if (globalreplysignum != -1)   FreeSignal(globalreplysignum);
  441.    if (globalfrontsignum != -1)   FreeSignal(globalfrontsignum);
  442.    if (IntuitionBase != NULL)      CloseLibrary((struct Library *)IntuitionBase);
  443.    if (GfxBase != NULL)            CloseLibrary((struct Library *)GfxBase);
  444.    if (inputDevPort != NULL)       DeletePort(inputDevPort);
  445.    if (stay && (port != NULL))     DeletePort(port);
  446.    {
  447.       struct hotkey *hk ;
  448.  
  449.       while (hotkeys) {
  450.          hk = hotkeys->next ;
  451.          FreeMem(hotkeys, (long)hotkeys->structlen) ;
  452.          hotkeys = hk ;
  453.       }
  454.    }
  455.    if (nullfh)                     Close(nullfh);
  456.    prito(0) ;
  457. }
  458.  
  459. struct IOStdReq *
  460. CreateIOReq(port, size)
  461. struct MsgPort *port;
  462. long size;
  463. {
  464.    register struct IOStdReq *ioReq;
  465.  
  466.    if ((ioReq = (struct IOStdReq *)
  467.                 AllocMem(size, MEMF_CLEAR | MEMF_PUBLIC)) != NULL) {
  468.       ioReq->io_Message.mn_Node.ln_Type = NT_MESSAGE;
  469.       ioReq->io_Message.mn_Node.ln_Pri  = 0;
  470.       ioReq->io_Message.mn_Length       = size;
  471.       ioReq->io_Message.mn_ReplyPort    = port;
  472.    }
  473.    return(ioReq);
  474. }
  475.  
  476. void DeleteIOReq(ioReq)
  477. register struct IOStdReq *ioReq;
  478. {
  479.    ioReq->io_Message.mn_Node.ln_Type = 0xff;
  480.    ioReq->io_Device = (struct Device *) -1;
  481.    ioReq->io_Unit = (struct Unit *) -1;
  482.    FreeMem( (char *)ioReq, (long)ioReq->io_Message.mn_Length);
  483. }
  484. /*
  485.  *   All of this stuff down here was written by Tomas Rokicki.
  486.  *   (C) Copyright 1987, 1988, Radical Eye Software.
  487.  */
  488. #include "graphics/gfxbase.h"
  489. /*
  490.  *   The maximum number of lines on the screen at once.
  491.  */
  492. #define MAXLINES (125)
  493. #define MAXPOINTS (4)
  494. int maxpoints ;
  495. /*
  496.  *   The external variables we access.
  497.  */
  498. struct RastPort *rastport ;
  499. short screenheight, screenwidth ;
  500. /*
  501.  *   Some locals to this file.
  502.  */
  503. static struct NewScreen newscreen = {
  504.    0, 0,
  505.    640, 400,
  506.    1,
  507.    0, 1,
  508.    HIRES | LACE | SCREENQUIET,
  509.    CUSTOMSCREEN,
  510.    NULL,
  511.    NULL,
  512.    NULL,
  513.    NULL } ;
  514. /*
  515.  *   This routine opens a screen and fires off the task if apropriate.
  516.  */
  517. void taskrout() ;
  518. blankscreen() {
  519.    register struct Task *rtask ;
  520.  
  521.    screenheight = 2 * GfxBase->NormalDisplayRows ;
  522.    screenwidth = GfxBase->NormalDisplayColumns ;
  523.    newscreen.Height = screenheight ;
  524.    newscreen.Width = screenwidth ;
  525.    if (globaldraw == -1 || AvailMem(MEMF_CHIP) < 70000L ||
  526.              (globalblankscreen = OpenScreen(&newscreen)) == NULL) {
  527.       if ((globalblankscreen = OpenScreen(&NewScreen)) != NULL) {
  528.          SetRGB4(&(globalblankscreen->ViewPort), 0L, 0L, 0L, 0L);
  529.          OFF_DISPLAY ;
  530.       }
  531.    } else {
  532.       if (globalblankscreen == NULL &&
  533.           (globalblankscreen = OpenScreen(&newscreen))==NULL)
  534.          return ;
  535. /*
  536.  *   Turning off the sprites is a little bit tricky.  A simple OFF_SPRITE
  537.  *   will continue to display the data in the current sprite registers.
  538.  *   This happens most often with the cursor.  To fix, we simply clear out
  539.  *   the sprite control registers after turning the sprites off.  This
  540.  *   might break all of the sprites when the system comes back up . . .
  541.  */
  542.       OFF_SPRITE ;
  543.       custom.spr[0].ctl = 0 ;
  544.       custom.spr[1].ctl = 0 ;
  545.       custom.spr[2].ctl = 0 ;
  546.       custom.spr[3].ctl = 0 ;
  547.       custom.spr[4].ctl = 0 ;
  548.       custom.spr[5].ctl = 0 ;
  549.       custom.spr[6].ctl = 0 ;
  550.       custom.spr[7].ctl = 0 ;
  551.       SetRGB4(&(globalblankscreen->ViewPort), 0L, 0L, 0L, 0L) ;
  552.       rastport = &(globalblankscreen->RastPort) ;
  553.       SetAPen(rastport, 0L) ;
  554.       Forbid() ;
  555.       RectFill(rastport, 0L, 0L, (long)screenwidth-1, 30L) ;
  556.       Permit() ;
  557.       SetAPen(rastport, 1L) ;
  558.       rtask = (struct Task *)AllocMem((long)sizeof(struct Task),
  559.                 MEMF_CLEAR | MEMF_PUBLIC) ;
  560.       if (rtask != NULL) {
  561.          rtask->tc_Node.ln_Pri = 10 ;
  562.          rtask->tc_Node.ln_Type = NT_TASK ;
  563.          rtask->tc_Node.ln_Name = "ri.Lines" ;
  564.          rtask->tc_SPLower = (APTR)stackmem ;
  565.          rtask->tc_SPUpper = rtask->tc_SPReg =
  566.                       (APTR)(stackmem + STACKSIZE/4 - 8) ;
  567.          AddTask(rtask, taskrout, 0L) ;
  568.          task = rtask ;
  569.       }
  570.    }
  571. }
  572. /*
  573.  *   Unblank the screen.  We kill the task with the standard ^C kill signal.
  574.  */
  575. screenunblank() {
  576.    if (task != NULL) {
  577.       Signal(task, 1L << SIGBREAKB_CTRL_C) ;
  578.       SetTaskPri(task, 11L) ;
  579.       Wait(1L << globalreplysignum) ;
  580.       RemTask(task);
  581.       FreeMem(task, (long)sizeof(struct Task)) ;
  582.       task = NULL ;
  583.    }
  584.    if (globalblankscreen != NULL) {
  585.       CloseScreen(globalblankscreen);
  586.       globalblankscreen = NULL ;
  587.       ON_DISPLAY ;
  588.       ON_SPRITE ;
  589.    }
  590. }
  591. /*
  592.  *   This routine returns a random value from 0 to n-1.
  593.  */
  594. int randm(i)
  595. int i ;
  596. {
  597.    static long seed ;
  598.    register long rseed ;
  599.    register long rval ;
  600.  
  601.    rseed = seed ;
  602.    if (rseed == 0)
  603.       rseed = 323214521 + globalblankscreen->MouseX +
  604.               globalblankscreen->MouseY ;
  605.    rseed = rseed * 123213 + 121 ;
  606.    rval = (rseed >> 5) & 65535 ;
  607.    seed = rseed ;
  608.    return ((i * rval) >> 16) ;
  609. }
  610. /*
  611.  *   This routine sets x and y values to a random number.
  612.  */
  613. static long x, y ;
  614. randomxy() {
  615.    x = randm(screenwidth) ;
  616.    y = randm(screenheight) ;
  617. }
  618. /*
  619.  *   Main routines are always fun.
  620.  */
  621. struct box {
  622.    short x[MAXPOINTS], y[MAXPOINTS] ;
  623. } store[MAXLINES] ;
  624. #define FIX(x) (((long)(x)) << 7)
  625. struct box *ptr ;
  626. struct box *eptr ;
  627. int numlines ;
  628. int mdelta = -1 ;
  629. int maxlines = MAXLINES/2 ;
  630. short dx[MAXPOINTS], dy[MAXPOINTS] ;
  631. short ox[MAXPOINTS], oy[MAXPOINTS] ;
  632. short nx[MAXPOINTS], ny[MAXPOINTS] ;
  633. short dr, dg, db ;
  634. short or, og, ob ;
  635. short nr, ng, nb ;
  636. /*
  637.  *   Draws a spline!  Expects all arguments in registers.
  638.  */
  639. #asm
  640.     public    _Draw
  641.     cseg
  642. rspline
  643.     move.l    a0,d0
  644.     sub.l    d6,d0
  645.     move.l    d0,d3
  646.     bpl    save1
  647.     neg.l    d0
  648. save1
  649.     move.l    a1,d1
  650.     sub.l    d7,d1
  651.     move.l    d1,d4
  652.     bpl    save2
  653.     neg.l    d1
  654. save2
  655.     move.l    d0,d2
  656.     cmp.l    d0,d1
  657.     bmi    save3
  658.     lsr.l    #3,d2
  659.     bra    save9
  660. save3
  661.     lsr.l    #3,d1
  662. save9
  663.     add.l    d1,d2
  664.     asr.l    #3,d2
  665.     asr.l    #5,d3
  666.     asr.l    #5,d4
  667.     move.l    a2,d0
  668.     sub.l    a0,d0
  669.     move.l    a3,d1
  670.     sub.l    a1,d1
  671.     asr.l    #5,d0
  672.     asr.l    #5,d1
  673.     muls.w    d4,d0
  674.     muls.w    d3,d1
  675.     sub.l    d1,d0
  676.     bpl    save4
  677.     neg.l    d0
  678. save4
  679.     cmp.l    d0,d2
  680.     bmi    pushem
  681.     move.l    a5,d0
  682.     sub.l    a0,d0
  683.     move.l    a6,d1
  684.     sub.l    a1,d1
  685.     asr.l    #5,d0
  686.     asr.l    #5,d1
  687.     muls.w    d4,d0
  688.     muls.w    d3,d1
  689.     sub.l    d1,d0
  690.     bpl    save5
  691.     neg.l    d0
  692. save5
  693.     cmp.l    d0,d2
  694.     bmi    pushem
  695. makeline
  696.     lsr.l    #7,d7
  697.     move.l    d7,-(sp)
  698.     lsr.l    #7,d6
  699.     move.l    d6,-(sp)
  700.     move.l    _rastport,-(sp)
  701.     jsr    _Draw
  702.     add.w    #12,a7
  703.     rts
  704. pushem
  705.     movem.l    d6/d7,-(sp)
  706.     move.l    a5,d0
  707.     add.l    d6,d0
  708.     asr.l    #1,d0
  709.     move.l    a6,d1
  710.     add.l    d7,d1
  711.     asr.l    #1,d1
  712.     movem.l    d0/d1,-(sp)
  713.     move.l    a2,d2
  714.     add.l    a5,d2
  715.     asr.l    #1,d2
  716.     move.l    a3,d3
  717.     add.l    a6,d3
  718.     asr.l    #1,d3
  719.     move.l    d0,d4
  720.     add.l    d2,d4
  721.     asr.l    #1,d4
  722.     move.l    d1,d5
  723.     add.l    d3,d5
  724.     asr.l    #1,d5
  725.     movem.l    d4/d5,-(sp)
  726.     move.l    a0,d6
  727.     add.l    a2,d6
  728.     asr.l    #1,d6
  729.     move.l    a1,d7
  730.     add.l    a3,d7
  731.     asr.l    #1,d7
  732.     move.l    d2,d0
  733.     add.l    d6,d0
  734.     asr.l    #1,d0
  735.     move.l    d3,d1
  736.     add.l    d7,d1
  737.     asr.l    #1,d1
  738.     move.l    d6,a2
  739.     move.l    d7,a3
  740.     move.l    d0,d6
  741.     add.l    d4,d6
  742.     asr.l    #1,d6
  743.     move.l    d1,d7
  744.     add.l    d5,d7
  745.     asr.l    #1,d7
  746.     movem.l    d6/d7,-(sp)
  747.     move.l    d0,a5
  748.     move.l    d1,a6
  749.     jsr    rspline
  750.     movem.l    (sp)+,a0/a1
  751.     movem.l    (sp)+,a2/a3/a5/a6
  752.     movem.l    (sp)+,d6/d7
  753.     bra    rspline
  754. #endasm
  755. /*
  756.  *   Now our linkage to the spline routine.  Parameters are in 8(a5)...
  757.  */
  758. int drawspline(x1, y1, x2, y2, x3, y3, x4, y4)
  759. long x1, y1, x2, y2, x3, y3, x4, y4 ;
  760. {
  761. #asm
  762.     movem.l    saver,-(sp)
  763.     move.l    8(a5),a0
  764.     move.l    12(a5),a1
  765.     move.l    16(a5),a2
  766.     move.l    20(a5),a3
  767.     move.l    28(a5),a6
  768.     move.l    32(a5),d6
  769.     move.l    36(a5),d7
  770.     move.l    24(a5),a5
  771.     jsr    rspline
  772.     movem.l    (sp)+,saver
  773. saver    reg    d0-d7/a0-a6
  774. #endasm
  775. }
  776. /*
  777.  *   Here we draw splines!  Magic, you know.
  778.  */
  779. dosplines(bptr)
  780. register struct box *bptr ;
  781. {
  782.    Move(rastport, (long)(bptr->x[0]), (long)(bptr->y[0])) ;
  783.    drawspline(FIX(bptr->x[0]), FIX(bptr->y[0]), FIX(bptr->x[1]),
  784.               FIX(bptr->y[1]), FIX(bptr->x[2]), FIX(bptr->y[2]),
  785.               FIX(bptr->x[3]), FIX(bptr->y[3])) ;
  786. }
  787. /*
  788.  *   Here we draw lines!
  789.  */
  790. dolines(bptr)
  791. register struct box *bptr ;
  792. {
  793.    Move(rastport, (long)(bptr->x[0]), (long)(bptr->y[0])) ;
  794.    Draw(rastport, (long)(bptr->x[1]), (long)(bptr->y[1])) ;
  795.    Draw(rastport, (long)(screenwidth-1-bptr->x[0]),
  796.                   (long)(screenheight-1-bptr->y[0])) ;
  797.    Draw(rastport, (long)(screenwidth-1-bptr->x[1]),
  798.                   (long)(screenheight-1-bptr->y[1])) ;
  799.    Draw(rastport, (long)(bptr->x[0]), (long)(bptr->y[0])) ;
  800. }
  801. /*
  802.  *   Here we draw a single line
  803.  */
  804. doline(bptr)
  805. register struct box *bptr ;
  806. {
  807.    Move(rastport, (long)(bptr->x[0]), (long)(bptr->y[0])) ;
  808.    Draw(rastport, (long)(bptr->x[1]), (long)(bptr->y[1])) ;
  809. }
  810. /*
  811.  *   Initialize things for the first lines.
  812.  */
  813. startlines() {
  814.    register int i ;
  815.  
  816.    ptr = store ;
  817.    eptr = store ;
  818.    numlines = 0 ;
  819.    if (dx[0] == 0) {
  820.       for (i=0; i<maxpoints; i++) {
  821.          ox[i] = randm(screenwidth) ;
  822.      oy[i] = randm(screenheight) ;
  823.      dx[i] = 2 + randm(3) ;
  824.      dy[i] = 2 + randm(3) ;
  825.       }
  826.    }
  827.    nr = 53 ;
  828.    ng = 33 ;
  829.    nb = 35 ;
  830.    dr = -3 ;
  831.    dg = 5 ;
  832.    db = 7 ;
  833.    SetRGB4(&(globalblankscreen->ViewPort), 0L, 0L, 0L, 0L) ;
  834.    SetRGB4(&(globalblankscreen->ViewPort), 1L, (long)(nr >> 3),
  835.                                  (long)(ng >> 3), (long)(nb >> 3)) ;
  836.    for (i=0; i<maxlines; i++) {
  837.       advancelines() ;
  838.       drawnew() ;
  839.    }
  840. }
  841. /*
  842.  *   Advance the number by the delta, and check the boundaries.
  843.  */
  844. adv(o, d, n, w)
  845. register short *o, *d, *n ;
  846. short w ;
  847. {
  848.    *n = *o + *d ;
  849.    if (*n < 0) {
  850.       *n = 0 ;
  851.       *d = randm(6) + 1 ;
  852.    } else if (*n >= w) {
  853.       *n = w - 1 ;
  854.       *d = - randm(6) - 1 ;
  855.    }
  856. }
  857. /*
  858.  *   Advance the two points which make up the lines.
  859.  */
  860. advancelines() {
  861.    register int i ;
  862.  
  863.    for (i=0; i<maxpoints; i++) {
  864.       adv(ox+i, dx+i, nx+i, screenwidth) ;
  865.       adv(oy+i, dy+i, ny+i, screenheight) ;
  866.    }
  867. }
  868. int (*drawfunc)() ;
  869. /*
  870.  *   Draw a new set of lines.
  871.  */
  872. drawnew() {
  873.    register int i ;
  874.    register short oldpen ;
  875.    register struct box *bptr ;
  876.  
  877.    while (numlines >= maxlines) {
  878.       oldpen = rastport->FgPen ;
  879.       SetAPen(rastport, 0L) ;
  880.       bptr = eptr ;
  881.       (*drawfunc)(bptr) ;
  882.       SetAPen(rastport, (long)oldpen) ;
  883.       numlines-- ;
  884.       bptr++ ;
  885.       if (bptr == store + MAXLINES)
  886.          bptr = store ;
  887.       eptr = bptr ;
  888.    }
  889.    bptr = ptr ;
  890.    for (i=0; i<maxpoints; i++) {
  891.       bptr->x[i] = ox[i] = nx[i] ;
  892.       bptr->y[i] = oy[i] = ny[i] ;
  893.    }
  894.    (*drawfunc)(bptr) ;
  895.    numlines++ ;
  896.    bptr++ ;
  897.    if (bptr == store + MAXLINES) {
  898.       bptr = store ;
  899.       if (mdelta == 1) {
  900.          maxlines++ ;
  901.          if (maxlines >= MAXLINES - 1)
  902.             mdelta = -1 ;
  903.       } else {
  904.          maxlines-- ;
  905.          if (maxlines <= 2)
  906.             mdelta = 1 ;
  907.       }
  908.    }
  909.    ptr = bptr ;
  910. }
  911. /*
  912.  *   This routine mucks with the colors.
  913.  */
  914. colors() {
  915.    or = nr ;
  916.    og = ng ;
  917.    ob = nb ;
  918.    adv(&or, &dr, &nr, 128) ;
  919.    adv(&og, &dg, &ng, 128) ;
  920.    adv(&ob, &db, &nb, 128) ;
  921.    SetRGB4(&(globalblankscreen->ViewPort), 1L, (long)(nr >> 3),
  922.                                     (long)(ng >> 3), (long)(nb >> 3)) ;
  923. }
  924. long oldtime[3] ;
  925. /*
  926.  *   Call this to set the time.
  927.  */
  928. void settime() {
  929.    DateStamp(oldtime) ;
  930. }
  931. /*
  932.  *   Call this to get the time since the last settime.
  933.  */
  934. long gettime() {
  935.    long t[3] ;
  936.  
  937.    DateStamp(t) ;
  938.    return(t[2]-oldtime[2] +
  939.          (t[1]-oldtime[1]) * 3000 +
  940.          (t[0]-oldtime[0]) * 4320000) ;
  941. }
  942. /*
  943.  *   Our actual task, in an infinite loop.
  944.  */
  945. int (*funcs[3])() = { &dolines, &dosplines, &doline } ;
  946. int maxp[3] = { 2, 4, 2 } ;
  947. int lastfunc = 0 ;
  948. void taskrout() {
  949.    register long i ;
  950.    register struct Task *task ;
  951.  
  952.    geta4() ;
  953.    settime() ;
  954.    lastfunc = lastfunc + 1 ;
  955.    if (lastfunc == 3)
  956.       lastfunc = 0 ;
  957.    drawfunc = funcs[lastfunc] ;
  958.    maxpoints = maxp[lastfunc] ;
  959.    task = FindTask(0L) ;
  960.    startlines() ;
  961.    i = 100 ;
  962.    colors() ;
  963.    while (SetSignal(0L, 0L)==0) {
  964.       if (task->tc_Node.ln_Pri == 10)
  965.          SetTaskPri(task, -20L) ;
  966.       advancelines() ;
  967.       drawnew() ;
  968.       advancelines() ;
  969.       drawnew() ;
  970.       if (task->tc_Node.ln_Pri == 10)
  971.          SetTaskPri(task, -20L) ;
  972.       advancelines() ;
  973.       drawnew() ;
  974.       advancelines() ;
  975.       drawnew() ;
  976.       if (task->tc_Node.ln_Pri == 10)
  977.          SetTaskPri(task, -20L) ;
  978.       advancelines() ;
  979.       drawnew() ;
  980.       advancelines() ;
  981.       drawnew() ;
  982.       colors() ;
  983.       if (--i <= 0) {
  984.          if (gettime() >= 15000) {
  985.             settime() ;
  986.             lastfunc = lastfunc + 1 ;
  987.             if (lastfunc == 3)
  988.                lastfunc = 0 ;
  989.             drawfunc = funcs[lastfunc] ;
  990.             maxpoints = maxp[lastfunc] ;
  991.         SetRast(rastport, 0L) ;
  992.         startlines() ;
  993.      }
  994.          i = 100 ;
  995.       }
  996.    }
  997. done:
  998.    Signal(globalbuddy, 1L << globalreplysignum) ;
  999.    Wait(0L) ;
  1000. }
  1001. /*
  1002.  *   Now we do hotkey magic to activate windows, bring them to front,
  1003.  *   etc.
  1004.  *
  1005.  *   Now we have a key, so we have to find a process with that name and
  1006.  *   bring her to front.  For now, we just deal with tasks, since the
  1007.  *   CLI stuff is so complicated.
  1008.  */
  1009. #define MAXMATCH (20)
  1010. char simplematch[3] = { ' ', '*', 0 } ;
  1011. struct Window *matchwindows[MAXMATCH] ;
  1012. extern long LockIBase() ;
  1013. windowtofront(key, qual)
  1014. char key ;
  1015. {
  1016.    long foo ;
  1017.    register int i, j ;
  1018.    int n ;
  1019.    register struct Window *w ;
  1020.    struct Screen *s ;
  1021.    struct Process *p ;
  1022.    struct MsgPort **mp ;
  1023.    struct CommandLineInterface *CLI ;
  1024.    int cli ;
  1025.    char *nameptr, *matchptr ;
  1026.    extern struct DosLibrary *DOSBase ;
  1027.    register struct hotkey *hk ;
  1028.    int shift, ctrl ;
  1029.    struct Window *activewindow ;
  1030.  
  1031. /*
  1032.  *   First we look for a matching record.
  1033.  */
  1034.    if (key == '.')
  1035.       goto goner ;
  1036.    shift = ((qual & (IEQUALIFIER_LSHIFT | IEQUALIFIER_RSHIFT)) != 0) ;
  1037.    ctrl = ((qual & IEQUALIFIER_CONTROL) != 0) ;
  1038.    activewindow = NULL ;
  1039.    for (hk=hotkeys; hk; hk=hk->next)
  1040.       if (hk->key == key && 
  1041.           (hk->flags & SHIFT) == shift)
  1042.          break ;
  1043.    cli = 0 ;
  1044.    if (hk && hk->matchstring)
  1045.       matchptr = hk->matchstring ;
  1046.    else {
  1047.       matchptr = simplematch ;
  1048.       if (key == ' ')
  1049.          matchptr++ ;
  1050.       else if ('A' <= key && key <= 'Z')
  1051.          simplematch[0] = key ;
  1052.       else if ('0' <= key && key <= '9') {
  1053.          cli = key ;
  1054.          if (cli == '0')
  1055.             cli += 10 ;
  1056.          matchptr++ ;
  1057.       } else
  1058.          goto goner ;
  1059.    }
  1060.    n = 0 ;
  1061.    foo = LockIBase(0L) ;
  1062.    if (! ctrl && !(hk && (hk->flags & NOTINTUITION)) && !cli) {
  1063.       for (s=IntuitionBase->FirstScreen; s; s=s->NextScreen)
  1064.          for (w=s->FirstWindow; w; w=w->NextWindow) {
  1065.             if (w->UserPort) {
  1066.                p = (struct Process *)(w->UserPort->mp_SigTask) ;
  1067.                if (((struct Task *)p)->tc_Node.ln_Type == NT_PROCESS) {
  1068.                   if (p->pr_CLI) {
  1069.                      CLI = (struct CommandLineInterface *)BSTRtoS(p->pr_CLI) ;
  1070.                      if (bstrcmp(BSTRtoS((CLI)->cli_CommandName), matchptr) &&
  1071.                          n < MAXMATCH) {
  1072.                            if (w==IntuitionBase->ActiveWindow)
  1073.                               activewindow = w ;
  1074.                            matchwindows[n++] = w ;
  1075.                         }
  1076.                   } else {
  1077.                      if (sstrcmp(((struct Task *)p)->tc_Node.ln_Name, matchptr)
  1078.                           && n < MAXMATCH) {
  1079.                         if (w==IntuitionBase->ActiveWindow)
  1080.                            activewindow = w ;
  1081.                         matchwindows[n++] = w ;
  1082.                      }
  1083.                   }
  1084.                }
  1085.             }
  1086.          }
  1087.    }
  1088. /*
  1089.  *   Folks, there are still several/many windows of vulnerability
  1090.  *   here; I'll have to plug them.  For instance, what happens if a
  1091.  *   CLI goes away while we are doing this?  Or what happens if a
  1092.  *   window goes away later, when we are looking at IntuitionBase?
  1093.  */
  1094.    if (! ctrl && !(hk && (hk->flags & NOTCLI))) {
  1095.       mp = (struct MsgPort **)BSTRtoS(*(DOSBase->dl_Root)) ;
  1096.       for (j=1; j<(long)(mp[0]); j++)
  1097.          if (mp[j] && (cli == 0 || j == cli - '0')) {
  1098.             p = (struct Process *)(mp[j]->mp_SigTask) ;
  1099.             CLI = (struct CommandLineInterface *)BSTRtoS(p->pr_CLI) ;
  1100.             if (!CLI->cli_Background &&
  1101.                   bstrcmp(BSTRtoS((CLI)->cli_CommandName), matchptr)) {
  1102.                infoptr->id_VolumeNode = NULL ;
  1103.                dos_packet(p->pr_ConsoleTask, ACTION_DISK_INFO,
  1104.                          ((long)(infoptr)) >> 2, 0L, 0L, 0L, 0L, 0L, 0L) ;
  1105.                if (w=(struct Window *)infoptr->id_VolumeNode)
  1106.                   if (n < MAXMATCH) {
  1107.                      if (w==IntuitionBase->ActiveWindow)
  1108.                         activewindow = w ;
  1109.                      matchwindows[n++] = w ;
  1110.                   }
  1111.             }
  1112.          }
  1113.    }
  1114.    if (shift)
  1115.       key += 100 ;
  1116.    if (ctrl || n==0) {
  1117.       UnlockIBase(foo) ;
  1118.       if (hk == NULL || hk->startstring == NULL)
  1119.          goto goner ;
  1120.       prito(0) ;
  1121.       Execute(hk->startstring, 0L, 0L) ;
  1122.       prito(20) ;
  1123.    } else {
  1124.       if (n == 1) {
  1125.          w = matchwindows[0] ;
  1126.       } else {
  1127. /*
  1128.  *   This is some real neat code.  We want to find the next window,
  1129.  *   that is, the window with the least address greater than the
  1130.  *   currently active window, unless the currently active window
  1131.  *   has the highest address, in which case we want to find the
  1132.  *   window with the least address.  Why this works is left as a
  1133.  *   puzzle for the reader.
  1134.  */
  1135.          if (activewindow) {
  1136.             w = activewindow ;
  1137.             for (j=0; j<n; j++)
  1138.                if ((w > activewindow) ^
  1139.                    (w > matchwindows[j]) ^
  1140.                    (matchwindows[j] > activewindow))
  1141.                   w = matchwindows[j] ;
  1142.          } else {
  1143.             w = NULL ;
  1144.             for (j=0; j<n; j++)
  1145.                if (lastwindows[key]==matchwindows[j]) {
  1146.                   w = lastwindows[key] ;
  1147.                   break ;
  1148.                }
  1149.             if (w==NULL)
  1150.                w = matchwindows[0] ;
  1151.          }
  1152.       }
  1153.       lastwindows[key] = w ;
  1154.       s = w->WScreen ;
  1155.       UnlockIBase(foo) ;
  1156.       ScreenToFront(s) ;
  1157. /*
  1158.  *   If only one window on screen, don't bring it to front
  1159.  *   (mostly for DPaint, but for other progs as well.)
  1160.  *   (Anyone know an easy way to see if this window is fully
  1161.  *   exposed?)
  1162.  */
  1163.       if (s->FirstWindow != w || w->NextWindow)
  1164.          WindowToFront(w) ;
  1165.       ActivateWindow(w) ;
  1166.    }
  1167.    return ;
  1168. goner:
  1169.    DisplayBeep(0L) ;
  1170. }
  1171. /*
  1172.  *   These two functions compare a given string `s' with a `key' string.
  1173.  *   The key string should be all upper case; this is a case insensitive
  1174.  *   match.  If the key string contains `*', this character is assumed to
  1175.  *   match the rest of the string (and it can only come at the end.)  We
  1176.  *   have a routine for BSTR's, and a routine for regular strings.
  1177.  */
  1178. int sstrcmp(s, key)
  1179. register char *s, *key ;
  1180. {
  1181.    while (1) {
  1182.       if (*key == '*')
  1183.          return(1) ;
  1184.       if (*key == 0)
  1185.          return(*s == 0) ;
  1186.       if (*s == 0)
  1187.          return(0) ;
  1188.       if (*s != *key &&
  1189.           (*s != *key + 32 || *s < 'a' || *s > 'z'))
  1190.          return(0) ;
  1191.       s++ ;
  1192.       key++ ;
  1193.    }
  1194. }
  1195. /*
  1196.  *   This is the same as above, only instead of using a null to
  1197.  *   end the string, we keep track of the number of characters.
  1198.  */
  1199. int bstrcmp(s, key)
  1200. register char *s, *key ;
  1201. {
  1202.    register int n ;
  1203.  
  1204.    n = *(unsigned char *)s++ ;
  1205.    while (1) {
  1206.       if (*key == '*')
  1207.          return(1) ;
  1208.       if (*key == 0)
  1209.          return(n == 0) ;
  1210.       if (n == 0)
  1211.          return(0) ;
  1212.       if (*s != *key &&
  1213.           (*s != *key + 32 || *s < 'a' || *s > 'z'))
  1214.          return(0) ;
  1215.       s++ ;
  1216.       key++ ;
  1217.       n-- ;
  1218.    }
  1219. }
  1220. /*
  1221.  *   A place to hold an input line.
  1222.  */
  1223. #define MAXSTARTUPLINE (100)
  1224. char startbuf[MAXSTARTUPLINE] ;
  1225. char upline[MAXSTARTUPLINE] ;
  1226. /*
  1227.  *   Is a legit separator of some sort.
  1228.  */
  1229. int issep(s)
  1230. register char s ;
  1231. {
  1232.    return (s <= ' ' || s == '=' || s == ':' || s == ',' || s == '-') ;
  1233. }
  1234. /*
  1235.  *   Go to next `word' in the startup file.
  1236.  */
  1237. char *getword(s)
  1238. register char *s ;
  1239. {
  1240.    while (*s && issep(*s))
  1241.       s++ ;
  1242.    return(s) ;
  1243. }
  1244. /*
  1245.  *   Upper case a string.
  1246.  */
  1247. char *upcase(dest, s)
  1248. char *dest ;
  1249. register char *s ;
  1250. {
  1251.    register char *d = dest ;
  1252.  
  1253.    while (*s) {
  1254.       if ('a' <= *s && *s <= 'z')
  1255.          *d++ = *s++ - 32 ;
  1256.       else
  1257.          *d++ = *s++ ;
  1258.    }
  1259.    *d = 0 ;
  1260.    return(dest) ;
  1261. }
  1262. /*
  1263.  *   Say we got a bad line.
  1264.  */
  1265. badline() {
  1266.    puts("Error in startup file!") ;
  1267.    puts(startbuf) ;
  1268. }
  1269. /*
  1270.  *   Copies a string from one place to another; string delimited by
  1271.  *   double quotes.
  1272.  */
  1273. char *cpystr(dest, src)
  1274. register char *dest, *src ;
  1275. {
  1276.    if (*src != '"')
  1277.       badline() ;
  1278.    else {
  1279.       src++ ;
  1280.       while (*src != '"' && *src != 0) {
  1281.          if (*src == '\\' && src[1] != 0)
  1282.             src++ ;
  1283.          *dest++ = *src++ ;
  1284.       }
  1285.       if (*src)
  1286.          src++ ;
  1287.    }
  1288.    *dest = 0 ;
  1289.    return(getword(src)) ;
  1290. }
  1291. /*
  1292.  *   Handle a single startup line that's not a comment and non-empty
  1293.  *   and been converted to all upper case.
  1294.  */
  1295. parseline(s, msg)
  1296. register char *s ;
  1297. struct OURMSG *msg ;
  1298. {
  1299.    int flags ;
  1300.    register short t ;
  1301.    register int key ;
  1302.    char *p ;
  1303.    register struct hotkey *hk ;
  1304.  
  1305.    flags = 0 ;
  1306.    if (strncmp(s, "COMMAND", 7)==0) {
  1307.       s = getword(s+7) ;
  1308.       cpystr(msg->cmd, s) ;
  1309.    } else if (strncmp(s, "TIMEOUT", 7)==0) {
  1310.       s = getword(s+7) ;
  1311.       t = 0 ;
  1312.       while ('0' <= *s && *s <= '9')
  1313.          t = t * 10 + *s++ - '0' ;
  1314.       if (t < 0)
  1315.          t = DEFTIME ;
  1316.       msg->interval = t ;
  1317.    } else if (strncmp(s, "LINES", 5)==0) {
  1318.       msg->draw = 1 ;
  1319.    } else if (strncmp(s, "BLANK", 5)==0) {
  1320.       msg->draw = -1 ;
  1321.    } else {
  1322.       if (strncmp(s, "SHIFT", 5)==0) {
  1323.          flags = SHIFT ;
  1324.          s = getword(s+5) ;
  1325.       }
  1326.       if (*s == 0)
  1327.          badline() ;
  1328.       else {
  1329.          if (issep(s[1]) &&
  1330.              (('A' <= *s && *s <= 'Z') ||
  1331.               ('0' <= *s && *s <= '9'))) {
  1332.             key = *s ;
  1333.             s = getword(s+1) ;
  1334.          } else if (strncmp(s, "SPACE", 5)==0) {
  1335.             key = ' ' ;
  1336.             s = getword(s+5) ;
  1337.          } else if (*s == 'F' && ('1' <= s[1] && s[1] <= '9')) {
  1338.             s++ ;
  1339.             t = *s++ - '0' ;
  1340.             if (t == 1 && *s == '0') {
  1341.                t = 10 ;
  1342.                s++ ;
  1343.             }
  1344.             key = t ;
  1345.             s = getword(s) ;
  1346.           } else {
  1347.             badline() ;
  1348.             return ;
  1349.          }
  1350.          if (strncmp(s, "INTUITION", 9)==0) {
  1351.             flags |= NOTCLI ;
  1352.             s = getword(s+9) ;
  1353.          } else if (strncmp(s, "CLI", 3)==0) {
  1354.             flags |= NOTINTUITION ;
  1355.             s = getword(s+3) ;
  1356.          }
  1357.          s = cpystr(upline, s) ;
  1358.          p = upline + strlen(upline) + 1 ;
  1359.          if (*s)
  1360.             cpystr(p, startbuf + (s-upline)) ;
  1361.          else
  1362.             *p = 0 ;
  1363.          t = sizeof(struct hotkey) + strlen(p) + strlen(upline) ;
  1364.          hk = AllocMem((long)t, MEMF_CLEAR | MEMF_PUBLIC) ;
  1365.          if (hk) {
  1366.             hk->key = key ;
  1367.             hk->flags = flags ;
  1368.             hk->next = hotkeys ;
  1369.         hk->structlen = t ;
  1370.         hk->matchstring = strcpy(hk->strings, upline) ;
  1371.         hk->startstring = strcpy(hk->strings + strlen(upline) + 1, p) ;
  1372.             if (hk->startstring[0]==0)
  1373.                hk->startstring = NULL ;
  1374.             hotkeys = hk ;
  1375.          } else
  1376.             puts("Out of memory in startup") ;
  1377.       }
  1378.    }
  1379. }
  1380. /*
  1381.  *   Handle the startup file.
  1382.  */
  1383. processstartup(s, msg)
  1384. char *s ;
  1385. struct OURMSG *msg ;
  1386. {
  1387.    FILE *f ;
  1388.    register char *p ;
  1389.  
  1390.    if (f=fopen(s, "r")) {
  1391.       while (fgets(startbuf, MAXSTARTUPLINE, f)) {
  1392.          p = getword(startbuf) ;
  1393.          if (*p != '*' && *p != '#' && *p != ';' && *p != 0) {
  1394.             upcase(upline, p) ;
  1395.             parseline(upline, msg) ;
  1396.          }
  1397.       }
  1398.       fclose(f) ;
  1399.    } else {
  1400.       puts("Couldn't open startup file:") ;
  1401.       puts(s) ;
  1402.    }
  1403. }
  1404. /*
  1405.  *   This stuff down here handles the raw key conversion stuff
  1406.  *   properly.  Thanks to Willy Langeveld and Carolyn Scheppner.
  1407.  */
  1408. char *dos_rkcv();
  1409. int  dos_rkcvinit(), dos_rkcvexit();
  1410. struct IOStdReq ConStdReq;
  1411. /*
  1412.  *   This code won't compile under Manx unless you delete the
  1413.  *   `ConsoleDevice' function in functions.h, or perform a kludge
  1414.  *   like the one I did when I included it.  Why the hell does
  1415.  *   Manx have that in there?  If you try to use it, it comes out
  1416.  *   undefined, but if you rename the following, it won't link
  1417.  *   because it needs the function `ConsoleDevice'.
  1418.  */
  1419. long ConsoleDevice ;
  1420. /**
  1421. *
  1422. *  Calling sequence:
  1423. *  =================
  1424. *
  1425. *    result = (char *) dos_rkcv(code, buffer, length);
  1426. *
  1427. *  Description:
  1428. *  ============
  1429. *
  1430. *    Covert raw key number to array of console device ascii text
  1431. *    using the default keymap.
  1432. *
  1433. *  Inputs:
  1434. *  =======
  1435. *
  1436. *    int code        Raw key number.
  1437. *    int qual        Qualifier.
  1438. *    char *buffer        Pointer to an array of char to receive the
  1439. *                conversion.
  1440. *    int length        length of buffer.
  1441. *
  1442. *  Outputs:
  1443. *  ========
  1444. *
  1445. *    F. value:        NULL on conversion failure, or pointer to
  1446. *                buffer on success.
  1447. *
  1448. **/
  1449. char *dos_rkcv(code, qual, buffer, length)
  1450. int code;
  1451. int qual;
  1452. char *buffer;
  1453. int length;
  1454. {
  1455.    static struct InputEvent event;
  1456.  
  1457.    event.ie_Class = IECLASS_RAWKEY;
  1458.    event.ie_Code = code;
  1459.    event.ie_Qualifier = qual;
  1460.  
  1461.    if (RawKeyConvert(&event, buffer, (long) length, NULL) == 0L) return(0L);
  1462.  
  1463.    return(buffer);
  1464. }
  1465.  
  1466. /**
  1467. *
  1468. *  Calling sequence:
  1469. *  =================
  1470. *
  1471. *    error = dos_rkcvinit();
  1472. *
  1473. *  Description:
  1474. *  ============
  1475. *
  1476. *    Open the Console device for later use with dos_rkcv().
  1477. *
  1478. *  Inputs:
  1479. *  =======
  1480. *
  1481. *    None
  1482. *
  1483. *  Outputs:
  1484. *  ========
  1485. *
  1486. *    F. value:        1 on failure, zero otherwise.
  1487. *                
  1488. *
  1489. **/
  1490. int dos_rkcvinit()
  1491. {
  1492.    if (OpenDevice("console.device", -1L, &ConStdReq, 0L) != NULL) {
  1493.       ConsoleDevice = 0L;
  1494.       return(1);
  1495.    }
  1496.    else {
  1497.       ConsoleDevice = (long) ConStdReq.io_Device;
  1498.       return(0);
  1499.    }
  1500. }
  1501.  
  1502. /**
  1503. *
  1504. *  Calling sequence:
  1505. *  =================
  1506. *
  1507. *    error = dos_rkcvexit();
  1508. *
  1509. *  Description:
  1510. *  ============
  1511. *
  1512. *    Close the Console device after use with dos_rkcv().
  1513. *
  1514. *  Inputs:
  1515. *  =======
  1516. *
  1517. *    None
  1518. *
  1519. *  Outputs:
  1520. *  ========
  1521. *
  1522. *    F. value:        Always zero;
  1523. *                
  1524. **/
  1525. int dos_rkcvexit()
  1526. {
  1527.    if (ConsoleDevice) CloseDevice(&ConStdReq);
  1528.    return(0);
  1529. }
  1530. /*
  1531.  *   Set up the key conversion table.  Note that the buffer has to be
  1532.  *   long word aligned!
  1533.  */
  1534. initkeytoasc() {
  1535.    register int i ;
  1536.    char buf[100] ;
  1537.  
  1538.    for (i=0; i<128; i++)
  1539.       keytoasc[i] = '.' ;
  1540.    if (dos_rkcvinit())
  1541.       return(1) ;
  1542.    for (i=0; i<128; i++) {
  1543.       buf[1] = 0 ;
  1544.       if (dos_rkcv(i, 0, buf, 100) && buf[1] == 0 &&
  1545.           (buf[0] == ' ' ||
  1546.            ('a' <= buf[0] && buf[0] <= 'z' && (buf[0] -= 32)) ||
  1547.            ('0' <= buf[0] && buf[0] <= '9'))) {
  1548.          keytoasc[i] = buf[0] ;
  1549.       }
  1550.    }
  1551. /*
  1552.  *   Have to handle the function keys separately
  1553.  */
  1554.    for (i=80; i<90; i++)
  1555.       keytoasc[i] = i-79 ;
  1556.    dos_rkcvexit() ;
  1557.    return(0) ;
  1558. }
  1559. /*
  1560.  *   Save a byte or two.
  1561.  */
  1562. _wb_parse() {}
  1563.